<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/6/12
     * Time: 下午12:52
     */

    namespace app\store\logic;


    use app\common\utils\UtilEncryption;
    use app\payment\logic\PayWechat;
    use app\store\model\OrderModel;
    use app\store\model\StoreOrder;
    use app\store\model\StoreOrderRefundModel;
    use think\Db;

    /**
     * 退款
     * Class RefundLogic
     * @package app\store\logic
     */
    class RefundLogic
    {

        static public function Apply($order_sn)
        {
            $OrderInfoModel = OrderModel::where(['order_sn' => $order_sn])->find();
            $OrderInfoModel->status = OrderModel::STATUS_USER_REFUND;
            return $OrderInfoModel->save();
        }


        /**
         * @param $order_sn
         * @return array
         * @throws \think\Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @throws \think\exception\PDOException
         */
        static public function refund($order_sn)
        {
            if ($order_sn == 0) {
                return ['error' => '缺少订单号!'];
            }
            if (StoreOrderRefundModel::where(['order_sn' => $order_sn])->find()) {
                return ['error' => '不可重复退款!'];
            }
            $orderinfo = StoreOrder::geIsBayOrderSnBuInfo($order_sn);
            if ($orderinfo) {
                if (!$orderinfo['pay_sn']) {

                    return ['error' => '订单未支付不能退款!'];
                }
                //创建退款订单记录
                $refund_order['pay_sn'] = $orderinfo['pay_sn'];
                $refund_order['order_sn'] = $orderinfo['order_sn'];
                $refund_order['pay_code'] = $orderinfo['pay_code'];
                $refund_order['buyer_id'] = $orderinfo['buyer_id'];
                $refund_order['pay_code'] = $orderinfo['pay_code'];
                $refund_order['seller_id'] = $orderinfo['seller_id'];
                $refund_order['address_id'] = $orderinfo['address_id'];
                $refund_order['goods_amount'] = StoreOrder::getOrderPaySumMoneyByPaySn($orderinfo['pay_sn']);
                $refund_order['refund_amount'] = $orderinfo['goods_amount'];
                $refund_order['transaction_id'] = $orderinfo['transaction_id'];
                $refund_order['order_refund_status'] = 0;
                StoreOrder::update(['order_status' => 21], ['order_sn' => $orderinfo['order_sn']]);
                if (StoreOrderRefundModel::create($refund_order)) {
                    return ['success' => '创建退款订单失败!',];
                } else {
                    return ['error' => '创建退款订单失败!',];
                }
            } else {
                return ['error' => '订单不存在或未付款!',];
            }
        }

        public function send($order_sn)
        {

            if ($order_sn == 0) {
                return ['error' => '缺少订单号!'];
            }
            $orderinfo = StoreOrder::geIsBayOrderSnBuInfo($order_sn);
            if ($orderinfo) {
                if (!$orderinfo['pay_sn']) {
                    return ['error' => '订单未支付不能退款!'];
                }
                //发起退款
                $refund_data['transaction_id'] = $orderinfo['transaction_id'];//微信生成的订单
                $refund_data['pay_sn'] = $orderinfo['pay_sn'];//商户自己生成的支付订单，
                $refund_data['out_refund_no'] = UtilEncryption::makeOrderSn($orderinfo['buyer_id']);;//退款订单号
                $refund_data['total_fee'] = StoreOrder::getOrderPaySumMoneyByPaySn($orderinfo['pay_sn']);//=0.01原始总支付金额
                $refund_data['refund_fee'] = $orderinfo['goods_amount'];//=0.01退还支付金额
                $refund_data['notify_url'] = '/api.php/store/storepay/collback';
                $refund_data['pay_app_id'] = MALL_APP_ID;
                $rest = PayWechat::refund($refund_data);
                if (isset($rest['weixin_respone'])) {
                    $result[] = Db::name('store_order_refund')->where(['order_sn' => $orderinfo['order_sn']])->update(['order_refund_status' => 1]);
                    return ['error' => '退款失败!', 'data' => $rest];
                } else {
                    Db::name('store_order')->where(['order_sn' => $orderinfo['order_sn']])->update(['order_status' => 22]);
                    return ['success' => '退款成功!', 'data' => $rest];
                }
            } else {
                return ['error' => '订单不存在!'];
            }
        }
    }